{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Number is a Sum of Powers of Three"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkPowersOfThree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #判断一个数字是否可以表示成三的幂的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>n</code> ，如果你可以将 <code>n</code> 表示成若干个不同的三的幂之和，请你返回 <code>true</code> ，否则请返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>对于一个整数 <code>y</code> ，如果存在整数 <code>x</code> 满足 <code>y == 3<sup>x</sup></code> ，我们称这个整数 <code>y</code> 是三的幂。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 12\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>12 = 3<sup>1</sup> + 3<sup>2</sup>\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 91\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>91 = 3<sup>0</sup> + 3<sup>2</sup> + 3<sup>4</sup>\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 21\n",
    "<b>输出：</b>false\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>7</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-number-is-a-sum-of-powers-of-three](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-number-is-a-sum-of-powers-of-three](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['12', '91', '21']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        if n==1:\n",
    "            return True\n",
    "        t = int(log(n,3)+0.01)\n",
    "        while t!=0:\n",
    "            n -= 3**t\n",
    "            if n==0 or n==1:\n",
    "                return True\n",
    "            if n<=0:\n",
    "                break\n",
    "            q = int(log(n,3)+0.01)\n",
    "            if q>=t:\n",
    "                break\n",
    "            t = q\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 > 1:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n>0:\n",
    "            mod=n%3\n",
    "            n //= 3\n",
    "            if mod==2:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n%3==2:\n",
    "                return False\n",
    "            n=n//3\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n%3 == 0:\n",
    "                n = n/3\n",
    "            elif n%3 == 1:\n",
    "                n = (n-1)/3\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while(n > 1):\n",
    "            if(n % 3 == 0):\n",
    "                n = n // 3\n",
    "            else:\n",
    "                if((n-1) % 3 != 0):\n",
    "                    return False\n",
    "                else:\n",
    "                    n = (n-1) // 3\n",
    "        return n == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while(n>=1):\n",
    "            y = int(math.log(n,3))\n",
    "            if  n == math.pow(3,y) * 3:\n",
    "                return True\n",
    "            elif n>= math.pow(3,y) * 2 :\n",
    "                return False\n",
    "            n -=  math.pow(3,y) \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n>0:\n",
    "            if n%3 == 2:\n",
    "                return False\n",
    "            n //=3\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        if n % 3 == 2:\n",
    "            return False\n",
    "        elif n // 3 >= 3:\n",
    "            return self.checkPowersOfThree(n // 3)\n",
    "        else:\n",
    "            return n // 3 < 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        temp = n\n",
    "        while temp > 0:\n",
    "            if temp % 3 == 2:\n",
    "                return False\n",
    "            temp = temp // 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        tmp = [3**i for i in range(15,-1,-1)]\n",
    "        for i in tmp:\n",
    "            if n >= i:\n",
    "                n -= i\n",
    "        return n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            n,x = divmod(n,3)\n",
    "            if x == 2:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        temp = n\n",
    "        while temp > 3:\n",
    "            if temp % 3 == 2:\n",
    "                return False\n",
    "            else:\n",
    "                temp = temp // 3\n",
    "        \n",
    "        if temp % 3 == 2:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        def f(n):\n",
    "            if n<2:\n",
    "                return True\n",
    "            if n==2:\n",
    "                return False\n",
    "            k=0\n",
    "            while(n>=3**k):\n",
    "                k=k+1\n",
    "            k=k-1\n",
    "            if n>=2*3**k:\n",
    "                return False\n",
    "            return f(n-3**k)\n",
    "        return f(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        # 3^15 > 10^7\n",
    "        power3 = [3**i for i in range(15)]\n",
    "\n",
    "        for mask in range(1 << 15):\n",
    "            \n",
    "            x = 0\n",
    "            for i in range(15):\n",
    "                if mask & (1 << i):\n",
    "                    x += power3[i]\n",
    "\n",
    "            if x == n:\n",
    "                return True\n",
    "\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        #十进制转换为三进制数\n",
    "        def dec_to_ter(num):\n",
    "            l = []\n",
    "            if num < 0:\n",
    "                return \"- \" + dec_to_ter(abs(num))#负数先转为正数，再调用函数主体\n",
    "            else:\n",
    "                while True:\n",
    "                    num,reminder = divmod(num,3)#算除法求除数和余数\n",
    "                    l.append(str(reminder))#将余数存入字符串\n",
    "                    if num == 0:\n",
    "                        return (l[::-1])\n",
    "        \n",
    "        for i in  dec_to_ter(n):\n",
    "            if i not in (\"0\",\"1\"):\n",
    "                return False\n",
    "        return True\n",
    "        \n",
    "\n",
    "\n",
    "            \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def checkPowersOfThree(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        while n> 0:\n",
    "            if n%3 >1:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n%3==2:\n",
    "                return False\n",
    "            n//=3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        if n==1:\n",
    "            return True\n",
    "        t = int(log(n,3)+0.01)\n",
    "        while t!=0:\n",
    "            n -= 3**t\n",
    "            print(t)\n",
    "            if n==0 or n==1:\n",
    "                return True\n",
    "            if n<=0:\n",
    "                break\n",
    "            q = int(log(n,3)+0.01)\n",
    "            if q>=t:\n",
    "                break\n",
    "            t = q\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n>0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //=3\n",
    "        \n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        \n",
    "        \n",
    "        while n:\n",
    "            x = n%3\n",
    "            if x == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while(n!=0 and (n%3==0 or n%3==1)):\n",
    "            n=n//3\n",
    "        \n",
    "        return n==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        # 找到最大的三的幂，使其不超过n\n",
    "        max_power = 0\n",
    "        while 3 ** (max_power + 1) <= n:\n",
    "            max_power += 1\n",
    "\n",
    "        # 从最大的三的幂开始尝试减去它们\n",
    "        for i in range(max_power, -1, -1):\n",
    "            if n >= 3 ** i:\n",
    "                n -= 3 ** i\n",
    "\n",
    "        # 如果我们成功地得到了0，那么返回True\n",
    "        return n == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        k = 0\n",
    "        while n >= 3 ** k:\n",
    "            if n == 3 ** k:\n",
    "                return True\n",
    "            k = k + 1\n",
    "\n",
    "\n",
    "        if n % 3 == 2:\n",
    "            return False\n",
    "\n",
    "        if n % 3 == 1:\n",
    "            n = n - 1\n",
    "        print(n, k)\n",
    "        for i in range(1, k):\n",
    "\n",
    "            if n % (3 ** i) != 0:\n",
    "\n",
    "                n = n - (3 ** (i - 1))\n",
    "                print(n)\n",
    "        print(n)\n",
    "        if n == 3**(k-1):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "\n",
    "        for j in range(17):\n",
    "            target = 3**(16-j)\n",
    "            if target<=n:\n",
    "                n-=target\n",
    "\n",
    "        if n == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        a=[1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969]\n",
    "        for i in range(len(a)-1,-1,-1):\n",
    "            if a[i]<=n:\n",
    "                n-=a[i]\n",
    "                if a[i]<=n:\n",
    "                    return False\n",
    "            if n==0:\n",
    "                return True    \n",
    "            if i==0:\n",
    "                return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        cnt = 3\n",
    "        while cnt < n:\n",
    "            cnt *= 3\n",
    "        while cnt > 0:\n",
    "            if n >= cnt:\n",
    "                n -= cnt\n",
    "            cnt //= 3\n",
    "        return n == 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        for i in range(16):\n",
    "            num = 3**i\n",
    "            if num > n:\n",
    "                max_num = i-1\n",
    "                break\n",
    "        while max_num > -1:\n",
    "            if 3 ** max_num <= n:\n",
    "                n = n - 3 ** max_num\n",
    "            max_num -= 1\n",
    "            if n == 0:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 > 1: return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n>0:\n",
    "            if n%3==2:\n",
    "                return False\n",
    "            n//=3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n>0:\n",
    "            if n%3==2:\n",
    "                return False\n",
    "            n//=3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "            n //= 3\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 进制转换\n",
    "    # 读题 不同的 三的幂之和\n",
    "    def checkPowersOfThree(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n % 3 == 2:\n",
    "                return False\n",
    "\n",
    "            n //= 3\n",
    "            \n",
    "        return True\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
